package com.mayuanfei.springboot11.controller;

import cn.hutool.core.util.StrUtil;
import com.mayuanfei.springboot11.common.R;
import com.mayuanfei.springboot11.pojo.UserBO;
import com.mayuanfei.springboot11.pojo.UserSignBO;
import com.mayuanfei.springboot11.validate.ValidGroup;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Email;

/**
 * Validator测试controller
 *
 * @author 老马
 * @date 2023-06-15 13:19
 */
@Validated
@RestController
public class ValidatorTestController {

    /**
     * RequestBody校验
     */
    @PostMapping("/test1")
    public R<UserBO> test1(@Validated @RequestBody UserBO userBO){
        return R.ok(userBO);
    }

    /**
     * Form表单校验
     */
    @PostMapping(value = "/test2")
    public R<UserBO> test2(@Validated UserBO userBO){
        return R.ok(userBO);
    }

    /**
     * 单个参数校验.当使用单参数校验时需要在Controller上加上@Validated注解，否则不生效
     */
    @GetMapping(value = "/test3")
    public R<Void> test3(@Email(message = "请填写正确的邮箱地址") String email){
        return R.ok(StrUtil.format("{}校验成功!", email));
    }

    @PostMapping(value = "/add")
    public R<Void> add(@Validated(value = ValidGroup.Create.class) @RequestBody UserBO userBO){
        return R.ok();
    }

    @PostMapping(value = "/update")
    public R<Void> update(@Validated(value = ValidGroup.Update.class) @RequestBody UserBO userBO){
        return R.ok();
    }

    @PostMapping(value = "/signCheck")
    public R<Void> signCheck(@Validated @RequestBody UserSignBO request){
        return R.ok();
    }
}